<template>
  <div class="box">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
    <div class="box1">
      <el-input
        placeholder="请输入内容"
        v-model="queryInfo.query"
        clearable
        @clear="getlist"
      ></el-input>
      <el-button icon="el-icon-search" @click="getlist"></el-button>
      <el-button type="primary" @click="add">添加用户</el-button>
      <el-table :data="tableData" border height="500">
        <el-table-column fixed type="index" label="#"> </el-table-column>
        <el-table-column prop="name" label="姓名"
          ><template slot-scope="scope"
            ><div>{{ scope.row.username }}</div></template
          ></el-table-column
        >
        <el-table-column prop="email" label="邮箱"
          ><template slot-scope="scope"
            ><div>{{ scope.row.email }}</div></template
          ></el-table-column
        >
        <el-table-column prop="mobile" label="电话"
          ><template slot-scope="scope"
            ><div>{{ scope.row.mobile }}</div></template
          ></el-table-column
        >
        <el-table-column prop="role_name" label="角色"
          ><template slot-scope="scope"
            ><div>{{ scope.row.role_name }}</div></template
          ></el-table-column
        >
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="switchstate(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="edit(scope.row.id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="del(scope.row.id)"
            ></el-button>
            <el-button
              type="warning"
              icon="el-icon-setting"
              size="mini"
              @click="setting(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[2, 4, 6, 8]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>

    <!-- 添加用户弹框 -->
    <el-dialog title="添加用户" :visible.sync="dialogFormVisible">
      <el-form :model="form" ref="ruleForm" :rules="rules">
        <el-form-item
          label="用户名"
          prop="username"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="密码"
          prop="password"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.password" placeholder="请输入密码">
          </el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email" :label-width="formLabelWidth">
          <el-input v-model="form.email" placeholder="请输入邮箱"> </el-input>
        </el-form-item>
        <el-form-item
          label="手机号"
          prop="mobile"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.mobile" placeholder="请输入手机号">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="adds">确 定</el-button>
      </div>
    </el-dialog>
     
    <!-- 修改用户信息弹窗 -->
    <el-dialog title="修改用户信息" :visible.sync="dlaog">
      <el-form :model="forms" ref="editruleForm" :editrules="editrules">
        <el-form-item
          label="用户名"
          prop="username"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="forms.username"
            placeholder="请输入用户名"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email" :label-width="formLabelWidth">
          <el-input v-model="forms.email" placeholder="请输入邮箱"> </el-input>
        </el-form-item>
        <el-form-item
          label="手机号"
          prop="mobile"
          :label-width="formLabelWidth"
        >
          <el-input v-model="forms.mobile" placeholder="请输入手机号">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dlaog = false">取 消</el-button>
        <el-button type="primary" @click="editadd">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 角色分类 -->
    <el-dialog title="分配角色" :visible.sync="dialogsetting">
      <el-form :model="formsetting" ref="settingruleForm">
        <el-form-item label="当前用户"
          >{{ formsetting.username }}
        </el-form-item>
        <el-form-item label="当前角色"
          >{{ formsetting.role_name }}
        </el-form-item>
        <el-form-item label="活动区域" :label-width="formLabelWidth">
          <el-select placeholder="请选择" v-model="selected">
            <el-option
              v-for="item in settingform"
              :key="item.id"
              :label="item.roleName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogsetting = false">取 消</el-button>
        <el-button type="primary" @click="settingadd">确 定</el-button>
      </div>
    </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      tableData: [], //用户列表数据
      value: true,
      total: 0,
      currentPage4: 4, //分页器
      //修改
      queryInfo: {
        query: "",
        pagenum: 1, //当前页码
        pagesize: 2, //页面条数
      },
      //添加数据参数
      form: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
      //编辑数据参数
      forms: {
        username: "",
        email: "",
        mobile: "",
      },
      //分配
      formsetting: {},
      settingform: [], //角色列表
      selected: "", //已选中角色的id
      formLabelWidth: "120px",
      dialogFormVisible: false, //添加确定
      dlaog: false, //编辑确定
      dialogsetting: false, //角色确定
      //添加用户正则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          { type: "email", message: "邮箱格式不正确", trigger: "blur" },
        ],

        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { min: 11, max: 11, message: "手机号码不正确", trigger: "blur" },
        ],
      },
      //编辑用户正则
      editrules: {
        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          { type: "email", message: "邮箱格式不正确", trigger: "blur" },
        ],

        mobile: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { min: 11, max: 11, message: "手机号码不正确", trigger: "blur" },
        ],
      },
    };
  },
  created() {},
  mounted() {
    this.getlist(); //调用表格内容
  },
  methods: {
    handleClick(row) {},
    //获取数据   搜索
    getlist() {
      this.$axios.get("users", { params: this.queryInfo }).then((res) => {
        // console.log(res);
        this.tableData = res.data.data.users;
        this.total = res.data.data.total;
      });
    },
    // 点击添加
    add() {
      this.dialogFormVisible = true;
    },
    //点击确定进行添加
    adds() {
      this.$refs.ruleForm.validate((valid) => {
        if (!valid) return this.$message.error("请填写完整用户信息");
        this.$axios.post("users", this.form).then((res) => {
          if (res.data.meta.status != 201) {
            this.$message.error("添加用户失败");
          } else {
            this.$message.success("添加用户成功");
            this.dialogFormVisible = false;
            this.getlist();
          }
          this.form = {};
        });
      });
    },
    //分页器
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getlist();
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.getlist();
    },
    //编辑用户
    edit(id) {
      this.dlaog = true;
      this.$axios.get("users/" + id).then((res) => {
        if (res.data.meta.status != 200) {
          return this.$message.error("获取用户信息失败");
        }
        this.forms = res.data.data;
      });
    },
    //确定编辑
    editadd() {
      this.$refs.editruleForm.validate((valid) => {
        if (!valid) return this.$message.error("请填写完整用户信息");
        this.$axios.put("users/" + this.forms.id, this.forms).then((res) => {
          if (res.data.meta.status != 200) {
            this.$message.error("修改用户失败");
          } else {
            this.$message.success("修改用户成功");
            this.dlaog = false;
            this.getlist();
          }
        });
      });
    },
    //删除用户
    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios.delete("users/" + id).then((res) => {
            if (res.data.meta.status != 200) {
              this.$message({
                type: "error",
                message: res.data.meta.msg,
              });
            } else {
              this.$message({
                type: "success",
                message: res.data.meta.msg,
              });
              this.getlist();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //分配用户
    setting(obj) {
      this.formsetting = obj;
      this.dialogsetting = true;
      this.$axios.get("roles").then((res) => {
        this.settingform = res.data.data;
      });
    },
    //确定更改角色
    settingadd() {
      if (!this.selected) {
        return this.$message.error("请选择要分配的角色");
      }
      this.$axios
        .put(`users/${this.formsetting.id}/role`, { rid: this.selected })
        .then((res) => {
          console.log(res);
          if (res.data.meta.status != 200) {
            this.$message.error("修改角色失败");
          } else {
            this.$message.success("修改角色成功");
            this.dialogsetting = false;
            this.getlist();
          }
        });
    },
    //修改状态
    switchstate(row) {
      console.log(row);
      this.$axios.put(`users/ ${row.id}}/state/${row.mg_state}`).then((res) => {
        // console.log(res);
        if (res.data.meta.status != 200) {
          this.$message.error("修改状态失败");
        } else {
          this.$message.success("修改状态成功");
        }
      });
    },
  },
};
</script>

<style scoped lang='scss'>
.box {
  width: 100%;
  height: 100%;
  .box1 {
    width: 100%;
    background-color: white;
    margin-top: 15px;
    .el-input {
      margin-left: 10px;
      width: 270px;
    }
  }
  .el-form-item {
    /deep/.el-form-item__label {
      text-align: left;
    }
  }
}
</style>